package com.zpark.drug.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpark.drug.common.ResultMapUtil;
import com.zpark.drug.pojo.Drug;
import com.zpark.drug.pojo.Supplier;
import com.zpark.drug.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;
    /**
     * 转向供应商页面
     * @return
     */
    @RequestMapping
    public String login(){
        return "/supplier";
    }
    /**
     * 转向供应商新增页面
     * @return
     */
    @RequestMapping(value = "/supplierPage")
    public String supplierPage(){
        return "/supplierPage";
    }
    /**
     * 转向供应商编辑页面
     * @return
     */
    @RequestMapping(value = "/supplierQueryById")
    public String supplierQueryById(@RequestParam(name = "id",required = true) Integer id, Model model){
        Supplier supplier = supplierService.findById(id);
        model.addAttribute("obj",supplier);
        return "/supplierPage";
    }
    /**
     * 获取所有供应商
     * @return
     */
    @RequestMapping(value = "/supplierList")
    @ResponseBody
    public Object supplierList(){
        List<Supplier> supplierList = supplierService.findAll();
        return ResultMapUtil.getHashMapList(supplierList);
    }

    /**
     * 添加一个供应商
     * @return
     */
    @RequestMapping(value = "/supplierAdd")
    @ResponseBody
    public Object supplierAdd(Supplier supplier){
        try {
            supplier.setCreatetime(new Date());
            int i = supplierService.addSupplier(supplier);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    @RequestMapping(value = "/supplierEdit")
    @ResponseBody
    public Object supplierEdit(Supplier supplier){
        try {
            int i = supplierService.updateSupplier(supplier);

            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个供应商
     * @return
     */
    @RequestMapping(value = "/supplierDelById")
    @ResponseBody
    public Object supplierDelById(Integer id){
        try {
            int i = supplierService.deleteSupplierById(id);

            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }
    /**
     * 分页查询供应商列表
     * @return
     */
    @RequestMapping(value = "/supplierQueryPage")
    @ResponseBody
    public Map<String, Object> supplierQueryPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String param) {
        PageInfo<Supplier> supplierPageInfo = supplierService.findPage(page, limit, param);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询数据成功");
        map.put("count", supplierPageInfo.getTotal());
        map.put("data", supplierPageInfo.getList());
        return map;
    }


}
